//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_UTILITY_UMEMBERASBASE_H_
#define _BUOLA_UTILITY_UMEMBERASBASE_H_

#include <buola/buola.h>

namespace buola {

///\addtogroup utility
///@{

/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// member as base
///
/// Encapsulates a type so that it can be used as a base class, when semantically it is actually
/// a member (a "contains" rather than "is a" semantic relationship)
///
/// Why would one want to do that?
///
///  - To enable the empty-base optimization (an empty base class occupies 0 bytes, while an
/// empty member occupies at least 1 byte, possibly more due to alignment issues)
///  - To be able to initialize a base class with a member (base classes are always initialized
/// before members, so it is not possible to initialize a member, then pass it to a base class
/// as an argument to the constructor. Making the member a base, and putting it before the "real"
/// base class makes it possible)
///
/// Why not just inherit from the member type?
///  - Sometimes it's not a class, so we can't inherit from it. If we are implementing a template,
/// we might not even know if it's a class
///  - It might contain virtual functions which have the same name as functions from the class we
/// are implementing. This would mean that we are overriding the virtual functions from our
/// member, which is certainly not what we want to do. This implementation will contain the member
/// as a member of the \c UMemberAsBase class, so functions will not be overridden
/// 
/// \tparam tType the type of the member we want to add as a base
/// \tparam vUniqueID if we need more than one member of the same type as bases, this allows to
/// differentiate them, so that we actually inherit from two different classes
/// \tparam vInherit unless you really know what you are doing, use the default value: \c true
/// for empty classes (as determined by std::is_empty) if vUniqueID==0, \c false for anything 
/// else, which includes any class with virtual functions, even if the class doesn't contain any 
/// non-static data member
///
/////////////////////////////////////////////////////////////////////////////////////////////////
template<typename tType,int vUniqueID=0,bool vInherit=(std::is_empty<tType>::value&&vUniqueID==0)>
class UMemberAsBase;

template<typename tType,int vUniqueID>
class UMemberAsBase<tType,vUniqueID,false>
{
public:
    ///returns the member
    tType &GetMember()              {   return mMember; }
    ///returns the member (const version)
    const tType &GetMember() const  {   return mMember; }

    ///forwarding constructor
    template<typename... tArgs>
    explicit UMemberAsBase(tArgs&&... pArgs)
        :   mMember(std::forward<tArgs>(pArgs)...)
    {}

protected:
    void SwapMember(UMemberAsBase &pRH)
    {
        using std::swap;
        swap(mMember,pRH.mMember);
    }

private:
    tType mMember;
};

template<typename tType,int vUniqueID>
class UMemberAsBase<tType,vUniqueID,true> : private tType
{
public:
    ///returns the member
    tType &GetMember()              {   return *this; }
    ///returns the member (const version)
    const tType &GetMember() const  {   return *this; }

    ///forwarding constructor
    template<typename... tArgs>
    explicit UMemberAsBase(tArgs&&... pArgs)
        :   tType(std::forward<tArgs>(pArgs)...)
    {}

protected:
    void SwapMember(UMemberAsBase &pRH)
    {
        using std::swap;
        swap((tType&)*this,(tType&)pRH);
    }
};

///returns a member embedded into a \c UMemberAsBase
template<typename tType,int vUniqueID,bool vInherit>
inline tType &member_as_base(UMemberAsBase<tType,vUniqueID,vInherit> &pBase)
{
    return pBase.GetMember();
}

///returns a member embedded into a \c UMemberAsBase (const version)
template<typename tType,int vUniqueID,bool vInherit>
inline const tType &member_as_base(const UMemberAsBase<tType,vUniqueID,vInherit> &pBase)
{
    return pBase.GetMember();
}

///@}

/*namespace buola*/ }

#endif
